<template>
	<div class="table-admin-padding layout-padding-view" style="height:65px;padding-top:15px;border:none">
		<div class="table-search-container" v-if="props.search.length > 0">
			<el-form ref="tableSearchRef" :model="state.form" size="default" label-width="60px" class="table-form">
				<el-row>
					<el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="4" v-for="(val, key) in search" :key="key" v-show="key <=  5 || state.isToggle">
						<template v-if="val.type !== ''">
							<el-form-item
								:label="val.label"
								:prop="val.prop"
								:rules="[{ required: val.required, message: `${val.label}不能为空`, trigger: val.type === 'input' ? 'blur' : 'change' }]"
							>
								<el-input v-model="state.form[val.prop]" :placeholder="val.placeholder" clearable v-if="val.type === 'input'" style="width: 100%" />
								<el-date-picker
									v-model="state.form[val.prop]"
									type="date"
									:placeholder="val.placeholder"
									v-else-if="val.type === 'date'"
									style="width: 100%"
								/>
								<el-select v-model="state.form[val.prop]" :placeholder="val.placeholder" v-else-if="val.type === 'select'" style="width: 100%">
									<el-option v-for="item in val.options" :key="item.value" :label="item.label" :value="item.value"> </el-option>
								</el-select>
							</el-form-item>
						</template>
					</el-col>
					<el-col :xs="24" :sm="12" :md="8" :lg="8" :xl="4">
						<el-form-item class="table-form-btn" :label-width="'20px' ">
							<div>
								<el-button type="primary" :icon="Search" @click="onSearch(tableSearchRef)">查询 </el-button>
								<el-button :icon="Refresh" type="info" class="ml10" style="background-color:#F48831; border-color:#F48831" @click="onReset(tableSearchRef)"> 重置 </el-button>
							</div>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
		</div>
	</div>
</template>

<script setup lang="ts" name="makeTableDemoSearch">
import { reactive, ref, onMounted } from 'vue';
import { Search, Refresh } from '@element-plus/icons-vue'

import type { FormInstance } from 'element-plus';
import { auth } from '/@/utils/authFunction';

// 定义父组件传过来的值
const props = defineProps({
	// 搜索表单
	search: {
		type: Array<TableSearchType>,
		default: () => [],
	},
});

// 定义子组件向父组件传值/事件
const emit = defineEmits(['add','search']);

// 定义变量内容
const tableSearchRef = ref<FormInstance>();
const state = reactive({
	form: {},
	isToggle: false,
});

// 查询
const onSearch = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.validate((valid: boolean) => {
		if (valid) {
			emit('search', state.form);
		} else {
			return false;
		}
	});
};
// 重置
const onReset = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	emit('search', state.form);
};
// 初始化 form 字段，取自父组件 search.prop
const initFormField = () => {
	if (props.search.length <= 0) return false;
	props.search.forEach((v) => (state.form[v.prop] = ''));
};
// 页面加载时
onMounted(() => {
	initFormField();
});
</script>

<style scoped lang="scss">
.table-search-container {
	display: flex;
	.table-form {
		flex: 1;
		.table-form-btn-toggle {
			white-space: nowrap;
			user-select: none;
			display: flex;
			align-items: center;
			color: var(--el-color-primary);
		}
	}
	.table-admin-padding {
		padding: 15px;
		.table-admin {
			flex: 1;
			overflow: hidden;
		}
	}
}
</style>
